package cn.mw.cmdb.viewProcessor;

import cn.mw.cmdb.entity.*;
import cn.mw.cmdb.param.ModelPropertyParam;
import cn.mw.cmdb.processor.ModelViewTypeContext;
import cn.mw.cmdb.processor.ModelViewTypeProccessor;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.ModelPropertyServiceImpl;

import java.util.*;

import static cn.mw.cmdb.entity.ModelInfo.prefix;
import static cn.mw.cmdb.viewProcessor.ModelViewCabinetProcess.*;
import static cn.mw.cmdb.viewProcessor.ModelViewRoomProcess.getRoomPropertyIds;

public class ModelViewUNumProcess implements ModelViewTypeProccessor {
    //所属机柜
    public static String relationCabinet =prefix +  "relationCabinet";
    //设备U位
    public static String deviceUIndex = prefix + "deviceUIndex";
    public static String deviceHeight = prefix + "deviceHeight";
    private static String relationCabinetKey = "上架机柜";
    private static String deviceUIndexKey = "设备U位";
    public static String deviceHeightKey = "设备高度";
    private static String UNumCatologName = "U位管理";

    @Override
    public Object genModelViewProperty(ModelViewTypeContext context) throws Exception {
        if (context != null && context.getParam() != null && (context.getParam() instanceof ModelInfo)) {
            ModelInfo param = (ModelInfo) context.getParam();
            Map<String, PropertyInfo> properties = param.getProperties();
            CmdbServiceManage cmdbServiceManage = context.getCmdbServiceManage();
            ModelPropertyServiceImpl service = (ModelPropertyServiceImpl) cmdbServiceManage.getModelService(ModelPropertyServiceImpl.ID);
            //清除其他视图对应的字段
            ModelPropertyParam propertyParam = editorModelPropertyField(param);
            service.doRemove(propertyParam);

            //内置机柜下属模型字段，新增，编辑模型时，自动生成
            PropertyInfo propertyInfo = new PropertyInfo();
            propertyInfo.setModelId(param.getId());
            propertyInfo.setName(relationRoomKey);
            propertyInfo.setEnableShow(true);
            propertyInfo.setDefaultListVisible(true);
            propertyInfo.setPropertyCatologName(UNumCatologName);
            PropertyTypeRef typeRef = new PropertyTypeRef();
            typeRef.setTypeId(PropertyTypeExtSingle.ID);
            propertyInfo.setPropertyTypeRef(typeRef);
            propertyInfo.setFieldViewAuthTypes(Set.of(FieldViewAuthType.add, FieldViewAuthType.upd, FieldViewAuthType.sel, FieldViewAuthType.list));
            propertyInfo.setId(relationRoom);
            propertyInfo.setSortNum(1);
            properties.put(propertyInfo.getId(), propertyInfo);

            propertyInfo = new PropertyInfo();
            propertyInfo.setModelId(param.getId());
            propertyInfo.setName(relationRoomAreaKey);
            propertyInfo.setEnableShow(true);
            propertyInfo.setDefaultListVisible(true);
            propertyInfo.setPropertyCatologName(UNumCatologName);
            typeRef = new PropertyTypeRef();
            typeRef.setTypeId(PropertyTypeExtSingle.ID);
            propertyInfo.setPropertyTypeRef(typeRef);
            propertyInfo.setFieldViewAuthTypes(Set.of(FieldViewAuthType.add, FieldViewAuthType.upd, FieldViewAuthType.sel, FieldViewAuthType.list));
            propertyInfo.setId(relationRoomArea);
            propertyInfo.setSortNum(2);
            properties.put(propertyInfo.getId(), propertyInfo);


            propertyInfo = new PropertyInfo();
            propertyInfo.setModelId(param.getId());
            propertyInfo.setName(relationCabinetKey);
            propertyInfo.setEnableShow(true);
            propertyInfo.setDefaultListVisible(true);
            propertyInfo.setPropertyCatologName(UNumCatologName);
            typeRef = new PropertyTypeRef();
            typeRef.setTypeId(PropertyTypeExtSingle.ID);
            propertyInfo.setPropertyTypeRef(typeRef);
            propertyInfo.setFieldViewAuthTypes(Set.of(FieldViewAuthType.add, FieldViewAuthType.upd, FieldViewAuthType.sel, FieldViewAuthType.list));
            propertyInfo.setId(relationCabinet);
            propertyInfo.setSortNum(3);
            properties.put(propertyInfo.getId(), propertyInfo);

            propertyInfo = new PropertyInfo();
            propertyInfo.setModelId(param.getId());
            propertyInfo.setName(deviceHeightKey);
            propertyInfo.setEnableShow(true);
            propertyInfo.setDefaultListVisible(true);
            propertyInfo.setPropertyCatologName(UNumCatologName);
            typeRef = new PropertyTypeRef();
            typeRef.setTypeId(PropertyTypeInt.ID);
            propertyInfo.setPropertyTypeRef(typeRef);
            propertyInfo.setFieldViewAuthTypes(Set.of(FieldViewAuthType.add, FieldViewAuthType.upd, FieldViewAuthType.sel, FieldViewAuthType.list));
            propertyInfo.setId(deviceHeight);
            propertyInfo.setSortNum(4);
            properties.put(propertyInfo.getId(), propertyInfo);


            propertyInfo = new PropertyInfo();
            propertyInfo.setModelId(param.getId());
            propertyInfo.setName(deviceUIndexKey);
            propertyInfo.setEnableShow(true);
            propertyInfo.setDefaultListVisible(true);
            propertyInfo.setPropertyCatologName(UNumCatologName);
            typeRef = new PropertyTypeRef();
            typeRef.setTypeId(PropertyTypeInt.ID);
            propertyInfo.setPropertyTypeRef(typeRef);
            propertyInfo.setFieldViewAuthTypes(Set.of(FieldViewAuthType.add, FieldViewAuthType.upd, FieldViewAuthType.sel, FieldViewAuthType.list));
            propertyInfo.setId(deviceUIndex);
            propertyInfo.setSortNum(5);
            properties.put(propertyInfo.getId(), propertyInfo);
        }
        return null;
    }

    @Override
    public Object addInstanceHandle(ModelViewTypeContext context) throws Exception {
        return null;
    }

    @Override
    public Object updInstanceHandle(ModelViewTypeContext context) throws Exception {
        return null;
    }

    /**
     * 清除U位管理视图对应的字段
     */
    public static ModelPropertyParam editorModelPropertyField(ModelInfo param) {
        ModelPropertyParam propertyParam = new ModelPropertyParam();
        Set<String> ids = new HashSet<>();
        getRoomPropertyIds(ids);
        getCabinetPropertyIds(ids);

        propertyParam.setIds(new ArrayList<>(ids));
        propertyParam.setModelId(param.getId());
        return propertyParam;
    }

    public static Set<String> getUNumTypePropertyIds(Set<String> ids) {
        ids.add(relationRoom);
        ids.add(relationRoomArea);
        ids.add(relationCabinet);
        ids.add(deviceHeight);
        ids.add(deviceUIndex);
        return ids;
    }
}
